//
//  DemoAyncTask.m
//  GT3Example
//
//  Created by NikoXu on 2019/12/10.
//  Copyright © 2019 Xniko. All rights reserved.
//

#import "DemoAyncTask.h"
#import "SCHttpManager.h"

@interface DemoAyncTask ()

@end

@implementation DemoAyncTask

- (void)executeValidationTaskWithValidateParam:(GT3ValidationParam *)param completion:(void (^)(BOOL, GT3Error * _Nullable))completion {
    /**
     *  TO-DO 处理result数据, 进行二次验证
     */
    NSDictionary *result = param.result;
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:result];
    [params setObject:@"login" forKey:@"business_type"];
    [params setObject:@{@"phone":self.phoneStr} forKey:@"business_param"];
  
    [[SCHttpManager shareInstance] httpPost:@"/userprod/geetest/validate" param:params completion:^(id response, NSError *error) {
      if (!error) {
          NSLog(@"通过业务流程");
          
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
              if (completion) {
                  completion(YES, nil);
              }
              if (self.delegate && [self.delegate respondsToSelector:@selector(asyncTaskDidFinish:error:)]) {
                  [self.delegate asyncTaskDidFinish:YES error:nil];
              }
          });
      }else{
          NSLog(@"无法通过业务流程");
          if (self.delegate && [self.delegate respondsToSelector:@selector(asyncTaskDidFinish:error:)]) {
              [self.delegate asyncTaskDidFinish:NO error:nil];
          }
      }
  }];
}

- (void)executeRegisterTaskWithCompletion:(void (^)(GT3RegisterParameter * _Nullable, GT3Error * _Nullable))completion {
    
    /**
     *  TO-DO 从接口解析是否开启极验3.0, 并解析和配置验证参数
     *  不要重复调用, 在交互上需要处理用户的短时间内多次点击的问题
     */
    NSString *ts = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970] * 1000];
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:ts forKey:@"t"];
    [param setObject:@"native" forKey:@"client_type"];
    [[SCHttpManager shareInstance] httpGet:@"/userprod/geetest/register" param:param completion:^(id response, NSError *error){
      if (response) {
          NSDictionary *param = response;
          if (!error && response) {
              NSString *geetest_id = [param objectForKey:@"gt"];
              NSString *geetest_challenge = [param objectForKey:@"challenge"];
              NSNumber *geetest_success = [param objectForKey:@"success"];
              //宕机模式
              if ([geetest_success isEqual:@(0)]) {
                  if (self.delegate && [self.delegate respondsToSelector:@selector(asyncTaskRegisterError)]) {
                      [self.delegate asyncTaskRegisterError];
                  }
              }else{
                  if (geetest_id && geetest_challenge && geetest_success) {
                      GT3RegisterParameter *param = [[GT3RegisterParameter alloc] init];
                      param.gt        = geetest_id;
                      param.challenge = geetest_challenge;
                      param.success   = geetest_success;

                      if (completion) {
                          completion(param, nil);
                      }
                  }
              }
          }
      }else{
          if (self.delegate && [self.delegate respondsToSelector:@selector(asyncTaskRegisterError)]) {
              [self.delegate asyncTaskRegisterError];
          }
      }
  }];
}

- (void)cancel {
}

@end
